package com.hospital.ssm.controller;

import com.hospital.ssm.dto.UserDto;
import com.hospital.ssm.model.ResultInfo;
import com.hospital.ssm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;

/**
 * @author chx
 */

@Controller
@CrossOrigin
@SessionAttributes(names = "username")
public class SysUserContoller {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/loginui")
    public String loginui() {
        return "/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultInfo pass(UserDto user, Model model) {
        ResultInfo re = new ResultInfo();
        try {
            UserDto userDto = sysUserService.login(user);
            UserDto us = sysUserService.logindoctor(user);
            if (userDto != null && us == null) {
                re.setOk();
                model.addAttribute("username", userDto.getUsername());
            } else if (us != null && userDto == null) {
                re.docsetOk();
                model.addAttribute("username", us.getUsername());
            } else if (us == null && userDto == null) {
                re.setError();
            }
        } catch (Exception e) {
            e.printStackTrace();
            re.setError();
        }
        return re;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session, SessionStatus sessionStatus) {
        session.removeAttribute("username");
        sessionStatus.setComplete();
        return "redirect:/loginui";
    }

    @GetMapping("/false")
    public String falses() {
        return "/false";
    }

    @RequestMapping("/registerui")
    public String register() {
        return "/register";
    }

    @PostMapping("/usernameExit")
    @ResponseBody
    public ResultInfo exit(String username) {
        ResultInfo re = new ResultInfo();
        String user = sysUserService.exit(username);
        String us = sysUserService.exitdoctor(username);
        if (user != null || us != null) {
            re.setError();
        } else {
            re.setOk();
        }
        return re;
    }

    @PutMapping("/adduser")
    @ResponseBody
    public ResultInfo adduser(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("usernamea") String usernamea, @RequestParam("passworda") String passworda) {
        UserDto user = new UserDto(username, password);
        UserDto usera = new UserDto(usernamea, passworda);
        System.out.println(user);
        System.out.println(usera);
        ResultInfo re = new ResultInfo();
        int n = 0;
        try {
            if (user != null && usera.getUsername().equals("")) {
                n = sysUserService.adduser(user);

            }
            if (usera != null && user.getUsername().equals("")) {
                n = sysUserService.adddoctoruser(usera);
            }
            if (n == 1) {
                re.setOk();
            } else {
                re.setError();
            }
        } catch (Exception e) {
            e.printStackTrace();
            re.setError();
        }
        return re;
    }
}
